package com.ruoyi.cm.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.base.domain.vo.BdTreeVo;
import com.ruoyi.system.domain.vo.SysOssVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cm.domain.vo.CmBsProcessInspectionVo;
import com.ruoyi.cm.domain.bo.CmBsProcessInspectionBo;
import com.ruoyi.cm.service.ICmBsProcessInspectionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序报验
 *
 * @author zxs
 * @date 2024-10-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cm/bsProcessInspection")
public class CmBsProcessInspectionController extends BaseController {

    private final ICmBsProcessInspectionService iCmBsProcessInspectionService;

    /**
     * 查询工序报验列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<CmBsProcessInspectionVo> list(CmBsProcessInspectionBo bo, PageQuery pageQuery) {
        return iCmBsProcessInspectionService.queryPageList(bo, pageQuery);
    }

    /**
     * 工序报验查询列表分页
     */
    @SaCheckLogin
    @GetMapping("/listQuery")
    public TableDataInfo<CmBsProcessInspectionVo> listQuery(CmBsProcessInspectionBo bo, PageQuery pageQuery) {
        return iCmBsProcessInspectionService.queryPageQueryList(bo, pageQuery);
    }

    /**
     * 导出工序报验列表
     */
    @SaCheckLogin
    @Log(title = "工序报验", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmBsProcessInspectionBo bo, HttpServletResponse response) {
        List<CmBsProcessInspectionVo> list = iCmBsProcessInspectionService.queryList(bo);
        ExcelUtil.exportExcel(list, "工序报验", CmBsProcessInspectionVo.class, response);
    }

    /**
     * 查询分部分项工序报验树
     */
    @SaCheckLogin
    @GetMapping("/fbfxGxTree")
    public R<List<BdTreeVo>> getFbfxGxTree(CmBsProcessInspectionBo bo) {
        return R.ok(iCmBsProcessInspectionService.getFbfxGxTree(bo));
    }

    /**H5端
     * 查询站点分部分项列表
     */
    @SaCheckLogin
    @GetMapping("/getZdFbfxList")
    public R<List<BdTreeVo>> getZdFbfxList(CmBsProcessInspectionBo bo) {
        return R.ok(iCmBsProcessInspectionService.getZdFbfxList(bo));
    }

    /**
     * 查询工序报验单的所有的附件。
     */
    @SaCheckLogin
    @GetMapping("/getGxbyFiles")
    public R<List<SysOssVo>> getGxbyFiles(CmBsProcessInspectionBo bo) {
        return R.ok(iCmBsProcessInspectionService.getGxbyFiles(bo));
    }

    /**
     * 获取工序报验详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<CmBsProcessInspectionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCmBsProcessInspectionService.queryById(id));
    }

    /**
     * 工序报验发起流程前校验
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/check/{id}")
    public R<Void> check( @PathVariable Long id) {
        iCmBsProcessInspectionService.check(id);
        return R.ok();
    }

    /**
     * 新增工序报验
     */
    @SaCheckLogin
    @Log(title = "工序报验", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmBsProcessInspectionBo bo) {
        return toAjax(iCmBsProcessInspectionService.insertByBo(bo));
    }

    /**
     * 批量新增工序报验
     */
    @SaCheckLogin
    @Log(title = "工序报验", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addBatch")
    public R<Void> addBatch( @RequestBody CmBsProcessInspectionBo bo) {
        return toAjax(iCmBsProcessInspectionService.addBatch(bo));
    }

    /**
     * 修改工序报验
     */
    @SaCheckLogin
    @Log(title = "工序报验", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmBsProcessInspectionBo bo) {
        return toAjax(iCmBsProcessInspectionService.updateByBo(bo));
    }

    /**
     * 删除工序报验
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "工序报验", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCmBsProcessInspectionService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
